"""
Case Type   : resource pool
Case Name   : 修改现有用户关联指定资源池
Create At   : 2022/08/29
Owner       : songjing20
Description :
    1.预置子class控制组及Workload控制组
    2.创建普通资源池关联workload_a1控制组
    3.创建组资源池关联class_b控制组和业务资源池关联workload_b1
    4.创建用户关联默认资源池
    5.修改用户关联普通资源池
    6.修改用户关联组资源池
    7.修改用户关联业务资源池
    8.环境清理
Expect      :
    1.创建成功
    2.创建成功
    3.创建成功
    4.创建成功
    5.成功
    6.成功
    7.成功
    8.清理成功
History     :
"""
import os
import unittest

from yat.test import Node
from yat.test import macro
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant


class ResourcePool(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f"-----{os.path.basename(__file__)} start-----")
        self.pri_node = Node('PrimaryDbUser')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.class_a = 'class_a'
        self.class_b = 'class_b'
        self.group_a = 'workload_a1'
        self.group_b = 'workload_b1'
        self.respool = [f'resp_resource_pool_0061_{i}' for i in range(1, 4)]
        self.user = [f'u_resource_pool_0061_{i}' for i in range(1, 4)]

    def test_resource_pool(self):
        text = "-----step1:预置子class控制组及Workload控制组;expect:创建成功-----"
        self.log.info(text)
        cmd = f"source {macro.DB_ENV_PATH};" \
              f"gs_cgroup -c -S {self.class_a} -s 20; " \
              f"gs_cgroup -c -S {self.class_a} -G {self.group_a} -g 20; " \
              f"gs_cgroup -c -S {self.class_b} -s 10; " \
              f"gs_cgroup -c -S {self.class_b} -G {self.group_b} -g 60; " \
              f"gs_cgroup -p"
        self.log.info(cmd)
        cmd_res = self.pri_node.sh(cmd).result()
        self.log.info(cmd_res)
        self.assertIn(f"CLASS  Name: {self.class_a}", cmd_res,
                      f'执行失败:{text}')
        self.assertIn(f"CLASS  Name: {self.class_b}", cmd_res,
                      f'执行失败:{text}')
        self.assertIn(f"DEFWD  Name: {self.group_a}", cmd_res,
                      f'执行失败:{text}')
        self.assertIn(f"DEFWD  Name: {self.group_b}", cmd_res,
                      f'执行失败:{text}')

        text = "-----step2:创建普通资源池关联workload_a1控制组;expect:创建成功-----"
        self.log.info(text)
        sql_res = self.pri_sh.execut_db_sql(
            f"create resource pool {self.respool[0]} "
            f"with(control_group = '{self.class_a}:{self.group_a}');")
        self.log.info(sql_res)
        self.assertEqual(self.constant.create_resource_pool_success, sql_res,
                         f'执行失败:{text}')

        text = "-----step3:创建组资源池关联class_b控制组和业务资源池关联workload_b1;" \
               "expect:创建成功-----"
        self.log.info(text)
        sql_res = self.pri_sh.execut_db_sql(
            f"create resource pool {self.respool[1]} "
            f"with(control_group = '{self.class_b}');"
            f"create resource pool {self.respool[2]} "
            f"with(control_group = '{self.class_b}:{self.group_b}');")
        self.log.info(sql_res)
        self.assertEqual(2, sql_res.count(
            self.constant.create_resource_pool_success), f'执行失败:{text}')

        text = "-----step4:创建用户关联默认资源池;expect:成功-----"
        self.log.info(text)
        sql_res = self.pri_sh.execut_db_sql(
            f"create user {self.user[0]} resource pool 'default_pool' "
            f"password '{macro.COMMON_PASSWD}';"
            f"create user {self.user[1]} password '{macro.COMMON_PASSWD}';"
            f"create user {self.user[2]} password '{macro.COMMON_PASSWD}';")
        self.log.info(sql_res)
        self.assertEqual(sql_res.count(self.constant.CREATE_ROLE_SUCCESS_MSG),
                         3, f'执行失败:{text}')

        text = "-----step5:修改用户关联普通资源池;expect:成功-----"
        self.log.info(text)
        sql_res = self.pri_sh.execut_db_sql(
            f"alter user {self.user[0]} resource pool '{self.respool[0]}';"
            f"select usename, respool from pg_user "
            f"where usename = '{self.user[0]}';")
        self.log.info(sql_res)
        self.assertIn(self.constant.ALTER_ROLE_SUCCESS_MSG,
                      sql_res, f'执行失败:{text}')
        self.assertIn(f"{self.user[0]} | {self.respool[0]}", sql_res,
                      f'执行失败:{text}')

        text = "-----step6:修改组用户关联组资源池;expect:成功-----"
        self.log.info(text)
        sql_res = self.pri_sh.execut_db_sql(
            f"alter user {self.user[1]} resource pool '{self.respool[1]}';"
            f"select usename, respool from pg_user "
            f"where usename = '{self.user[1]}';")
        self.log.info(sql_res)
        self.assertIn(self.constant.ALTER_ROLE_SUCCESS_MSG,
                      sql_res, f'执行失败:{text}')
        self.assertIn(f"{self.user[1]} | {self.respool[1]}", sql_res,
                      f'执行失败:{text}')

        text = "-----step7:修改用户关联业务资源池;expect:成功-----"
        self.log.info(text)
        sql_res = self.pri_sh.execut_db_sql(
            f"alter user {self.user[2]} resource pool '{self.respool[2]}' "
            f"user group '{self.user[1]}';"
            f"select usename, respool from pg_user "
            f"where usename = '{self.user[2]}';")
        self.log.info(sql_res)
        self.assertIn(self.constant.ALTER_ROLE_SUCCESS_MSG,
                      sql_res, f'执行失败:{text}')
        self.assertIn(f"{self.user[2]} | {self.respool[2]}", sql_res,
                      f'执行失败:{text}')

    def tearDown(self):
        text = "-----step8:环境清理;expect:成功-----"
        self.log.info(text)
        drop_user = self.pri_sh.execut_db_sql(
            '\n'.join(
                [f"drop user if exists {i} cascade;" for i in self.user]))
        self.log.info(drop_user)
        drop_resp_res = self.pri_sh.execut_db_sql(
            '\n'.join(
                [f"drop resource pool if exists {i};" for i in self.respool]))
        self.log.info(drop_resp_res)
        cmd = f"source {macro.DB_ENV_PATH};gs_cgroup --revert"
        self.log.info(cmd)
        cmd_res = self.pri_node.sh(cmd).result()
        self.assertEqual(drop_user.count(self.constant.DROP_ROLE_SUCCESS_MSG),
                         3, f'执行失败:{text}')
        self.assertEqual(3, drop_resp_res.count(
            self.constant.drop_resource_pool_success), f'执行失败:{text}')
        self.assertEqual('', cmd_res, f'执行失败:{text}')
        self.log.info(f"-----{os.path.basename(__file__)} end-----")
